package com.gsm.service.impl;

import com.gsm.dao.ChapterDao;
import com.gsm.dao.CourseDao;
import com.gsm.entity.Result;
import com.gsm.po.Chapter;
import com.gsm.po.Course;
import com.gsm.service.ChapterService;
import com.gsm.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程章节(Chapter)表服务实现类
 */
@Service
public class ChapterServiceImpl implements ChapterService {
    @Autowired
    private ChapterDao chapterDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private IdUtils idUtils;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public Result insertChapter(Chapter chapter) {
        Integer showOrder = chapterDao.selectMaxShowOrderBySubjectId(chapter.getSubjectId());
        long chapterId = idUtils.nextId();
        chapter.setChapterId(chapterId);
        chapter.setShowOrder(showOrder == null ? 0 : ++showOrder);
        chapterDao.insertChapter(chapter);
        return Result.success();
    }

    /**
     * 根据 主键 chapterId 删除
     *
     * @param chapterId 主键
     * @return
     */
    @Override
    public Result deleteChapterByChapterId(Long chapterId) {
        chapterDao.deleteChapterByChapterId(chapterId);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param chapter 实例对象
     * @return
     */
    @Override
    public Result updateChapterByChapterId(Chapter chapter) {
        chapterDao.updateChapterByChapterId(chapter);
        return Result.success();
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param chapterId 主键
     * @return
     */
    @Override
    public Result selectChapterByChapterId(Long chapterId) {
        Chapter chapter = chapterDao.selectChapterByChapterId(chapterId);
        Result result = Result.success(chapter);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param chapter 实例对象
     * @return
     */
    @Override
    public Result selectChapterList(Chapter chapter) {
        List<Chapter> list = chapterDao.selectChapterList(chapter);
        Result result = Result.success(list);
        return result;
    }

    @Override
    public Result selectChapterListBySubjectId(Long subjectId) {
        Chapter chapter = new Chapter();
        chapter.setSubjectId(subjectId);
        List<Chapter> list = chapterDao.selectChapterList(chapter);
        Result result = Result.success(list);
        return result;
    }

    @Override
    public Result selectChapterListAndCourseListBySubjectId(Long subjectId) {
        Chapter chapter = new Chapter();
        chapter.setSubjectId(subjectId);
        List<Chapter> list = chapterDao.selectChapterList(chapter);
        if (list != null) {
            list.forEach(i -> {
                List<Course> courseList = courseDao.selectCourseListByChapterId(i.getChapterId());
                i.setCourseList(courseList);
            });
        }
        return Result.success(list);
    }

    @Override
    public Result deleteChapterByChapterIds(Long subjectId, List<Long> ids) {
        chapterDao.deleteChapterByChapterIds(ids);
        return Result.success();
    }

    @Override
    public Result updateChapterShowOrder(Long subjectId, List<Long> ids) {
        chapterDao.updateChapterShowOrder(ids);
        return Result.success();
    }
}